﻿using FastExecutor.Base.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastExecutor.Base.Util
{
    public class TreeBuilder
    {
        public static List<Tree> Build(string TopNodeParentGuid, List<Tree> AllTreeList)
        {
            List<Tree> TopNodeList = AllTreeList.Where(a => a.paerntGuid == TopNodeParentGuid).ToList();
            foreach (Tree item in TopNodeList)
            {
                BuildSubNode(item, AllTreeList);
            }
            return TopNodeList;
        }
        private static void BuildSubNode(Tree ParentNode, List<Tree> AllTreeList)
        {
            List<Tree> FilterList = AllTreeList.Where(a => a.paerntGuid == ParentNode.id).ToList();
            if (FilterList.Count>0)
            {
                ParentNode.hasChildren = true;
                List<Tree> ChildrenList = new List<Tree>(); 
                foreach (Tree item in FilterList)
                {
                    item.hasParent = true;
                    BuildSubNode(item, AllTreeList);
                    ChildrenList.Add(item);
                }
                ParentNode.children = ChildrenList;
            }
         
        }
    }
}
